import request from '@/utils/request'
import Qs from 'qs'

//-------------登录权限接口-------------
export function login(data) {
  data.email = data.username
  return request({
    url: '/passport/login', // '/vue-element-admin/user/login',
    method: 'post',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
    transformRequest: [function (data) {
      let newData = '';
      for (let k in data) {
          if (Object.prototype.hasOwnProperty.call(data, k) === true) {
              newData += encodeURIComponent(k) + '=' + encodeURIComponent(data[k]) + '&';
          }
      }
      return newData;
    }],
    data
  })
}

export function getInfo(token) {
  return request({
    url: '/general/menu', // url: '/vue-element-admin/user/info',
    method: 'get',
    params: { token }
  })
}

export function logout() {
  return request({
    url: '/passport/logout', // url: '/vue-element-admin/user/logout',
    method: 'post'
  })
}

export function switchSpace(id) {
  return request({
    url: '/space/' + id + '/switch',
    method: 'put',
    headers: { 'Content-Type': 'application/json;charset=utf-8' },
    data: {}
  })
}

export function getSystemInfo() {
  return request({
    url: '/general/info',
    method: 'get'
  })
}

//-------------用户管理接口-------------
export function getUsers(size, page, kw) {
  return request({
    url: '/user/?size=' + size + ((page) ? '&page=' + page : '') + ((kw && kw != '') ? '&kw=' + kw : ''),
    method: 'get'
  })
}

export function addUser(username, email, password) {
  return request({
      url: '/user/',
      method: 'post',
      headers: { 'Content-Type': 'application/x-www-form-urlencoded, multipart/form-data' },
      transformRequest: [function(data) {
        data = Qs.stringify(data)
        return data
      }],
      data: {
        email: email,
        password: password,
        username: username
      }
  })
}

export function updateUser(user_id, username, email, password) {
  return request({
      url: '/user/' + user_id,
      method: 'put',
      headers: { 'Content-Type': 'application/x-www-form-urlencoded, multipart/form-data' },
      transformRequest: [function(data) {
        data = Qs.stringify(data)
        return data
      }],
      data: {
        username: username,
        email: email,
        password: password
      }
  })
}

export function updateUserGeneralInfo(user_id, username, avatar) {
  return request({
      url: '/user/' + user_id,
      method: 'put',
      headers: { 'Content-Type': 'application/x-www-form-urlencoded, multipart/form-data' },
      transformRequest: [function(data) {
        data = Qs.stringify(data)
        return data
      }],
      data: {
        username: username,
        avatar: avatar
      }
  })
}

export function updateUserPassword(user_id, password) {
  return request({
      url: '/user/' + user_id,
      method: 'put',
      headers: { 'Content-Type': 'application/x-www-form-urlencoded, multipart/form-data' },
      transformRequest: [function(data) {
        data = Qs.stringify(data)
        return data
      }],
      data: {
        password: password
      }
  })
}

export function deleteUser(user_id) {
  return request({
      url: '/user/' + user_id,
      method: 'delete',
      headers: { 'Content-Type': 'application/json' }
  })
}

